package com.leosmart

import android.animation.ObjectAnimator
import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.OvershootInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.graphics.drawable.toDrawable
import androidx.core.view.WindowCompat
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import com.facebook.react.views.view.setStatusBarTranslucency
import com.facebook.react.views.view.setStatusBarVisibility


class MainActivity : ReactActivity() {
    companion object {
        private var instance: MainActivity? = null
        fun getInstance(): MainActivity? = instance
    }
    private lateinit var mSplashDialog: Dialog;

    /**
   * Returns the name of the main component registered from JavaScript. This is used to schedule
   * rendering of the component.
   */
  override fun getMainComponentName(): String = "tsun smart pro"

    /**
     * Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
     * which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
     */
    override fun createReactActivityDelegate(): ReactActivityDelegate {
        return super.createReactActivityDelegate()
    }
    @RequiresApi(Build.VERSION_CODES.O)
    fun dismissSplash(color: Color) {
        if (::mSplashDialog.isInitialized && mSplashDialog.isShowing) {
            val contentView = mSplashDialog.window?.decorView?.findViewById<ViewGroup>(android.R.id.content)
            val fadeOut = ObjectAnimator.ofInt(window!!.decorView.background.mutate(), "alpha", 255, 0)
            fadeOut.duration = 500 // 动画时长，单位毫秒
            fadeOut.interpolator = AccelerateDecelerateInterpolator()
            fadeOut.start()
            contentView?.let { viewGroup ->
                // 开始淡出动画
                viewGroup.animate()
                    ?.alpha(0f)
                    ?.setDuration(500)
                    ?.withEndAction {
                        window.setBackgroundDrawable(color.toDrawable())
                        mSplashDialog.dismiss()
                    }
                    ?.start()
            } ?: run {
                // 找不到视图时直接移除背景后 dismiss
                window.setBackgroundDrawable(color.toDrawable())
                mSplashDialog.dismiss()
            }
        }
    }
    fun createSplashView(){
        if (!this.isFinishing) {
            mSplashDialog = Dialog(this,R.style.SplashScreenTheme)
            mSplashDialog.window?.apply {
                setLayout(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
                
                // 设置状态栏和导航栏为透明
                statusBarColor = Color.TRANSPARENT
                navigationBarColor = Color.TRANSPARENT
                
                // 设置窗口类型为应用程序窗口
                setType(WindowManager.LayoutParams.TYPE_APPLICATION)
                
                // 设置窗口标志 - 关键设置
                addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
                addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
                
                // 确保 Dialog 显示在最顶层
                attributes?.apply {
                    flags = flags or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                }
            }
            
            val launchView=layoutInflater.inflate(R.layout.splash, null)
            mSplashDialog.setContentView(launchView)
            mSplashDialog.setCancelable(false)
            
            // 在显示后确保沉浸式设置生效
            mSplashDialog.setOnShowListener {
                mSplashDialog.window?.apply {
                    // 使用更现代的方法设置沉浸式状态栏
                    decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            or View.SYSTEM_UI_FLAG_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
                    
                    // 确保状态栏和导航栏透明
                    statusBarColor = Color.TRANSPARENT
                    navigationBarColor = Color.TRANSPARENT
                }
            }
            
            if (!mSplashDialog.isShowing) {
                mSplashDialog.show()
                startLaunchAnimation(launchView)
            }
        }
    }
    fun hideStatusBar(activity: Activity) {
        val window = activity.window
        window.setStatusBarVisibility(false)
        window.setStatusBarTranslucency(true)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        window.setBackgroundDrawable(Color.WHITE.toDrawable())
        // 在 super.onCreate 之前设置
        super.onCreate(null)
        instance = this
        hideStatusBar(this)
        this.runOnUiThread { this.createSplashView() }
    }
    private fun startLaunchAnimation(view: View) {
        val title = view.findViewById<TextView>(R.id.title_text)
        val logo = view.findViewById<ImageView>(R.id.logo_image)
        val layoutParams = logo.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.topMargin = (resources.displayMetrics.heightPixels * 0.25).toInt()
        logo.layoutParams = layoutParams

        // 文字淡入 + 缩放
        title.apply {
            scaleX = 0.8f
            scaleY = 0.8f
            alpha = 0f
            animate()
                .alpha(1f)
                .scaleX(1f)
                .scaleY(1f)
                .setStartDelay(300)
                .setDuration(1500)
                .setInterpolator(OvershootInterpolator())
                .start()
        }

        // Logo 缩放动画
        logo.apply {
            scaleX = 0.5f
            scaleY = 0.5f
            animate()
                .scaleX(1f)
                .scaleY(1f)
                .setStartDelay(100)
                .setDuration(1500)
                .setInterpolator(OvershootInterpolator())
                .start()
        }
    }
}
